From c64d191d20dca89cd20a9f704a7805e0316ee7ab Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 4 Aug 2014 07:21:31 -0700 Subject: [PATCH] Allow documenting binary targets. This removes the check in the compilation phase, but adds an error if you're documenting a library and a binary with the same name (as the rustdoc output would conflict). --- src/cargo/ops/cargo_doc.rs | 27 ++++++++++++++++++- src/cargo/ops/cargo_rustc/mod.rs | 6 ----- tests/test_cargo_doc.rs | 45 ++++++++++++++++---------------- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 6252b2831..1b3264e66 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -1,5 +1,9 @@ +use std::collections::HashSet; + +use core::source::Source; use ops; -use util::CargoResult; +use sources::PathSource; +use util::{CargoResult, human}; pub struct DocOptions<'a> { pub all: bool, @@ -8,6 +12,27 @@ pub struct DocOptions<'a> { pub fn doc(manifest_path: &Path, options: &mut DocOptions) -> CargoResult<()> { + let mut source = PathSource::for_path(&manifest_path.dir_path()); + try!(source.update()); + let package = try!(source.get_root_package()); + + let mut lib_names = HashSet::new(); + let mut bin_names = HashSet::new(); + for target in package.get_targets().iter().filter(|t| t.get_profile().is_doc()) { + if target.is_lib() { + assert!(lib_names.insert(target.get_name())); + } else { + assert!(bin_names.insert(target.get_name())); + } + } + for bin in bin_names.iter() { + if lib_names.contains(bin) { + return Err(human("Cannot document a package where a library and a \ + binary have the same name. Consider renaming one \ + or marking the target as `doc = false`")) + } + } + try!(ops::compile(manifest_path, &mut options.compile_opts)); Ok(()) } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 8e8e80482..91c23b77b 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -237,12 +237,6 @@ fn prepare_rustc(package: &Package, target: &Target, crate_types: Vec<&str>, fn rustdoc(package: &Package, target: &Target, cx: &mut Context) -> Work { - // Can't document binaries, but they have a doc target listed so we can - // build documentation of dependencies even when `cargo doc` is run. - if target.is_bin() { - return proc() Ok(()) - } - let kind = KindTarget; let pkg_root = package.get_root(); let cx_root = cx.layout(kind).proxy().dest().dir_path().join("doc"); diff --git a/tests/test_cargo_doc.rs b/tests/test_cargo_doc.rs index cb89187f8..5442887e9 100644 --- a/tests/test_cargo_doc.rs +++ b/tests/test_cargo_doc.rs @@ -27,36 +27,17 @@ test!(simple { assert_that(&p.root().join("target/doc/foo/index.html"), existing_file()); }) -test!(no_build_main { +test!(doc_no_libs { let p = project("foo") .file("Cargo.toml", r#" [package] name = "foo" version = "0.0.1" authors = [] - "#) - .file("src/lib.rs", r#" - pub fn foo() {} - "#) - .file("src/main.rs", r#" - bad code - "#); - assert_that(p.cargo_process("cargo-doc"), - execs().with_status(0).with_stdout(format!("\ -{compiling} foo v0.0.1 (file:{dir}) -", - compiling = COMPILING, - dir = p.root().display()).as_slice())); -}) - -test!(doc_no_libs { - let p = project("foo") - .file("Cargo.toml", r#" - [package] + [[bin]] name = "foo" - version = "0.0.1" - authors = [] + doc = false "#) .file("src/main.rs", r#" bad code @@ -212,4 +193,24 @@ test!(doc_only_bin { assert_that(&p.root().join("target/doc"), existing_dir()); assert_that(&p.root().join("target/doc/bar/index.html"), existing_file()); + assert_that(&p.root().join("target/doc/foo/index.html"), existing_file()); +}) + +test!(doc_lib_bin_same_name { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", "fn main() {}") + .file("src/lib.rs", "fn foo() {}"); + + assert_that(p.cargo_process("cargo-doc"), + execs().with_status(101) + .with_stderr("\ +Cannot document a package where a library and a binary have the same name. \ +Consider renaming one or marking the target as `doc = false` +")); }) -- 2.30.2